//
//  MineViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/15.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "MineViewController.h"
#import "MinePersonInformationCell.h"
#import "MineOrderCell.h"
#import "MineListCell.h"
#import "MineDiscountViewController.h"
#import "MineRechargeViewController.h"
#import "PersonalViewController.h"
#import "AboutViewController.h"
#import "OpinionViewController.h"
#import "BusinessViewController.h"
#import "CollectionViewController.h"
#import "MineAllOrderViewController.h"
#import "MineProblemViewController.h"
#import "RuleViewController.h"
@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT - 49 - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[MinePersonInformationCell class] forCellReuseIdentifier:@"person"];
    [_tableView registerClass:[MineOrderCell class] forCellReuseIdentifier:@"order"];
    [_tableView registerClass:[MineListCell class] forCellReuseIdentifier:@"list"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, 60)];
    [self.view addSubview:_tableView];
    

}


#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
        return 304;
    else if (indexPath.row == 1)
        return 152;
    else
        return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        MinePersonInformationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"person"];
        [cell.discountBtn addTarget:self action:@selector(handleDiscountBtn:) forControlEvents:UIControlEventTouchUpInside];
        [cell.rechargeBtn addTarget:self action:@selector(handleRechargeBtn:) forControlEvents:UIControlEventTouchUpInside];
        [cell.collectBtn addTarget:self action:@selector(handleCollectionBtn:) forControlEvents:UIControlEventTouchUpInside];
        [cell.headerBtn addTarget:self action:@selector(handleHeadAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.ruleBtn addTarget:self action:@selector(handleRuleBtn) forControlEvents:UIControlEventTouchUpInside];
        [cell configView];
        cell.viewController = self;
        return cell;
    }
    else if (indexPath.row == 1)
    {
        MineOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"order"];
        cell.viewController = self;
        return cell;
    }
    else
    {
        MineListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"list"];
        
        switch (indexPath.row) {
            case 2:
                cell.iv.image = IMAGE(@"mine_opinion");
                cell.label.text = @"意见反馈";
                break;
            case 3:
                cell.iv.image = IMAGE(@"mine_cooperation");
                cell.label.text = @"商务合作";
                break;
            case 4:
                cell.iv.image = IMAGE(@"mine_about");
                cell.label.text = @"关于我们";
                break;
            case 5:
                cell.iv.image = IMAGE(@"mine_problem");
                cell.label.text = @"常见问题";
                break;
            case 6:
                cell.iv.image = IMAGE(@"mine_clear_cache");
                cell.label.text = @"清除缓存";
                cell.cacheLabel.hidden = NO;
                cell.cacheLabel.text = [self getAppCache];
                break;
            case 7:
                cell.iv.image = IMAGE(@"mine_log_out");
                cell.label.text = @"退出登录";
                break;
                
            default:
                break;
        }
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 2:
        {
            OpinionViewController *opinionVC = [OpinionViewController new];
            opinionVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:opinionVC animated:YES];
        }
            break;
        case 3:
        {
            BusinessViewController *buVC = [BusinessViewController new];
            buVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:buVC animated:YES];
        }
            break;
        case 4:
        {
            AboutViewController *aboutVC = [AboutViewController new];
            aboutVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:aboutVC animated:YES];
        }
            break;
        case 5:
        {
            MineProblemViewController *problem = [MineProblemViewController new];
            problem.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:problem animated:YES];
        }
            break;
        case 6:
        {
         AlertViewManager *alert = [[AlertViewManager alloc] initWithTitle:@"是否清除缓存?" message:nil cancelButtonTitle:@"取消" confirmButtonTitle:@"确定" delegate:self block:^(NSInteger idx) {
             if(idx == 1)
             {
                 [self clearCache];
             }
             
         }];
            [alert show];
        }
            break;
        case 7:
        {
            AlertViewManager *alert = [[AlertViewManager alloc] initWithTitle:@"是否退出登录?" message:nil cancelButtonTitle:@"取消" confirmButtonTitle:@"确定" delegate:self block:^(NSInteger idx) {
                if(idx == 1)
                {
                    [GW_Tool clearLoginInfo];
                    [self backToHomeTab:0];

                }
            }];
            [alert show];
        }
            break;
            
        default:
            break;
    }
}


//了解规则
- (void)handleRuleBtn
{
    RuleViewController *ruleVC = [RuleViewController new];
    ruleVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:ruleVC animated:YES];
}

//优惠券
- (void)handleDiscountBtn:(UIButton *)sender
{
    MineDiscountViewController *mineVC = [MineDiscountViewController new];
    mineVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mineVC animated:YES];
}

//充值派乐宝
- (void)handleRechargeBtn:(UIButton *)sender
{
    MineRechargeViewController *rechargeVC = [MineRechargeViewController new];
    rechargeVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:rechargeVC animated:YES];
}

- (void)handleCollectionBtn:(UIButton *)sender
{
    NSLog(@"收藏");
    CollectionViewController *collection = [CollectionViewController new];
    collection.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:collection animated:YES];
}

//点击头像
- (void)handleHeadAction
{
    PersonalViewController *personVC = [PersonalViewController new];
    personVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:personVC animated:YES];
}


- (void)viewWillAppear:(BOOL)animated
{
    [_tableView reloadData];
}

//获取缓存大小
- (NSString *)getAppCache {
    
    float tmpSize = [[SDImageCache sharedImageCache] getSize];
    
    // 1k = 1024, 1m = 1024k
    if (tmpSize < 1024 * 100) {// 小于100k
        return @"";// 不显示
    }else if (tmpSize < 1024 * 1024){// 小于1m
        return [NSString stringWithFormat:@"%.0fK",tmpSize/1024];
    }else if (tmpSize < 1024 * 1024 * 1024){// 小于1G
        return [NSString stringWithFormat:@"%.1fM",tmpSize/(1024 * 1024)];
    }else{
        return [NSString stringWithFormat:@"%.1fG",tmpSize/(1024*1024*1024)];
    }
    
}

//清除缓存
- (void)clearCache {
    
    //    60 * 60 * 24 * 7  一周
    
    [SDImageCache sharedImageCache].maxCacheAge = 0;// 默认清除一周之前的图片，设置为0，清除所有图片
    
    [SVProgressHUD show];
    @weakobj(self);
    [[SDImageCache sharedImageCache] cleanDiskWithCompletionBlock:^{
        
        [SVProgressHUD dismiss];
        @strongobj(self);
        [self.tableView reloadData];
        
        [SDImageCache sharedImageCache].maxCacheAge = 60 * 60 * 24 * 7;// 一周
    }];
    
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
